
layui.use(['element','jquery','form','table','layer','laydate','layedit'],function () {
    var element = layui.element;
    var $=layui.jquery;
    var form=layui.form;
    var table=layui.table;
    var layer=layui.layer;
    var laydate=layui.laydate;
    var layedit=layui.layedit;

    //日期时间选择器
    laydate.render({
        elem: '#startTime'
        ,type: 'datetime'
    });
    //日期时间选择器
    laydate.render({
        elem: '#endTime'
        ,type: 'datetime'
    });


    var tableIns = table.render({
        elem: '#booksTable',
        url: '/admin/books/booksList',
        toolbar: '#booksToolBar',
        page: 'true',
        title: '书籍数据表',
        cols: [ [
            {type:'checkbox'},
            {field:'id',title:'ID',align:'center'},
            {field:'bookName',title:'书名',align:'center'},
            {field:'bookDescribe',title:'描述',align:'center'},
            {field:'price',title:'价格',align:'center'},
            {field:'isdiscount', title:'是否打折', templet: function(d) {
                    return '<div>'+d.isdiscount==''?"异常":d.isdiscount==1?"不打折":d.isdiscount==2?"已打折":"异常"+'</div>';
                },align:'center'},
            {field:'discount',title:'折扣价',align:'center'},
            {field:'state',title:'状态',templet: function(d) {
                    return '<div>'+d.state==''?"异常":d.state==1?"发布中":d.state==2?"交易中":d.state==3?"交易成功":d.state==4?"已下架":"异常"+'</div>';
                },align:'center'},
            {field:'createTime',title:'创建时间',align:'center'},
            {field:'changeTime',title:'修改时间',align:'center'},
            {title:'操作',toolbar:'#booksRowBar',width:200}
        ] ],
        done:function (res,curr,count) {
            if(curr>1 && res.data.length==0){
                tableIns.reload({
                    page:{curr:curr-1}
                });

            }
        }
    });



    form.on("submit(doSearch)",function (data) {
        var select = [] ;
        $(".classifyId select option:selected").each(function(){
            if($(this).val()==''){
                return;
            }
            if($(this).val()){
                select.push($(this).val());
            }
        });
        console.log(select)
        data.field.classifyId=select.join(",");
        tableIns.reload({
            url:'/admin/books/booksList',
            where:data.field,
            page:{
                curr:1
            }
        })
        return false;
    })


    table.on('tool(booksTable)',function (obj) {
        var data = obj.data;
        switch (obj.event) {
            case "soldout":
                soldoutInfo(data);
                break;
            case "show":
                showNotice(data);
                break;
        }
    })

    //弹出层索引
    var mainIndex;
    //提交路径
    var url;
    //初始化富文本编辑器
    var editIndex;

    //查看书籍
    function  showNotice(data) {
        mainIndex = layer.open({
            type:1,
            content:$("#showNoticeDiv"),
            area:["700px","500px"],
            title:"查看书籍",
            success:function () {
                $.get("/admin/books/selectById",{"id":data.id},function (res){
                    if(res.success){
                        $("#dataEnd").empty();
                        var bookData = res.data;
                        console.log(bookData)
                        $("#show_bookName").html(bookData.bookName);
                        $("#show_bookDescribe").html(bookData.bookDescribe);
                        $("#show_publisherName").html(bookData.publisherName);
                        $("#show_createtime").html(bookData.createTime);
                        $("#show_price").html(bookData.price);
                        $("#show_isdiscount").html(bookData.isdiscount==''?"异常":bookData.isdiscount==1?"不打折":bookData.isdiscount==2?"已打折":"异常")
                        $("#show_state").html(bookData.state==''?"异常":bookData.state==1?"发布中":bookData.state==2?"交易中":bookData.state==3?"交易成功":bookData.state==4?"已下架":"异常")
                        $("#show_discount").html(bookData.discount)
                        $("#show_purchaserName").html(bookData.purchaserName==null?"无":bookData.purchaserName)
                        $("#show_img").attr("src",bookData.imageUrl);

                        var html="";
                        for (var i = 0 ; i < bookData.pac.length;i++){
                            html+="<p>"+bookData.pac[i].parentName+" <b>:</b> <span>"+bookData.pac[i].childName+"</span></p>";
                        }
                        console.log(html)
                        $("#dataEnd").html(html);
                    }else{
                        layer.msg(res.content);
                        layer.close(mainIndex);
                    }
                },"json")
            }
        });
    }



    //下架
    function soldoutInfo(data){
        if(data.state=="3"||data.state=="4"){
            layer.msg("该数据无法下架")
            return
        }
        layer.confirm("确定要下架这书籍吗？",{icon:3,title:"提示"},function (index) {
            $.post("/admin/books/soldout",{"id":data.id,"state":data.state},function (result) {
                if(result.success){
                    tableIns.reload();
                    layer.msg("已下架");
                }
                layer.msg("下架失败");
            },"json")
            //关闭弹窗
            layer.close(index);
        })
    }

});
